package com.lx.sum;

import com.lx.entitys.CovidSumEntity;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: chenjiang
 * @Date: 2021/11/18/16:56
 * @Description:
 */
public class CovidSumReducer extends Reducer<Text, CovidSumEntity, Text, CovidSumEntity> {
    private CovidSumEntity reduceOutValues = new CovidSumEntity();

    /***
     *
     * @param key map端输出的key
     * @param values map端输出 的数据集
     * @param context 上下文
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<CovidSumEntity> values, Context context) throws IOException, InterruptedException {
        long totalCases = 0;
        long totalDeaths = 0;
        for (CovidSumEntity entity : values) {
            totalCases += entity.getCases();
            totalDeaths += entity.getDeaths();
        }
        reduceOutValues.setCases(totalCases);
        reduceOutValues.setDeaths(totalDeaths);
        context.write(key, reduceOutValues);
    }
}
